--- title: EPSCN: Real-Time Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolution Neural keywords: fastai sidebar: home_sidebar ---
{% raw %}
{% endraw %} {% raw %}
%reload_ext autoreload
%autoreload 2
%matplotlib inline
{% endraw %} {% raw %}
import os
import cv2
import numpy as np
import re
import random
from tqdm import tqdm
from matplotlib import pyplot as plt
import PIL
{% endraw %} {% raw %}
{% endraw %} {% raw %}
import sys
sys.path.append('..')
from superres.datasets import *
from superres.databunch import *
{% endraw %} {% raw %}
seed = 8610
random.seed(seed)
np.random.seed(seed)
{% endraw %}

Model

{% raw %}
# https://github.com/nyk510/srcnn-pytorch
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class EPSCN[source]

EPSCN(upscale=2) :: Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:to, etc.

{% endraw %} {% raw %}
test_input=torch.ones(1, 1, 64, 64)
g = EPSCN(4)
test_input = test_input.cuda()
g = g.cuda()
out = g(test_input)
print(out.size())
torch.Size([1, 1, 256, 256])
{% endraw %}

DataBunch

{% raw %}
train_hr = div2k_train_hr_crop_256
{% endraw %} {% raw %}
in_size = 64
out_size = 256
scale = 4
bs = 10
{% endraw %} {% raw %}
data = create_sr_databunch(train_hr, in_size=in_size, out_size=out_size, scale=scale, convert_mode='YCbCr', bs=bs, seed=seed)
print(data)
data.show_batch(cmap='gray')
ImageDataBunch;

Train: LabelList (25245 items)
x: ImageImageList
Image (1, 64, 64),Image (1, 64, 64),Image (1, 64, 64),Image (1, 64, 64),Image (1, 64, 64)
y: ImageImageList
Image (1, 256, 256),Image (1, 256, 256),Image (1, 256, 256),Image (1, 256, 256),Image (1, 256, 256)
Path: /home/jovyan/notebook/datasets/DIV2K/DIV2K_train_HR_crop/256;

Valid: LabelList (6311 items)
x: ImageImageList
Image (1, 64, 64),Image (1, 64, 64),Image (1, 64, 64),Image (1, 64, 64),Image (1, 64, 64)
y: ImageImageList
Image (1, 256, 256),Image (1, 256, 256),Image (1, 256, 256),Image (1, 256, 256),Image (1, 256, 256)
Path: /home/jovyan/notebook/datasets/DIV2K/DIV2K_train_HR_crop/256;

Test: None
{% endraw %}

Training

{% raw %}
model = EPSCN(upscale=4)
loss_func = MSELossFlat()
metrics = [m_psnr, m_ssim]
learn = Learner(data, model, loss_func=loss_func, metrics=metrics)
learn.path = Path('.')
model_name = model.__class__.__name__
{% endraw %} {% raw %}
lr_find(learn)
learn.recorder.plot(suggestion=True)
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Min numerical gradient: 1.10E-06
Min loss divided by 10: 2.29E-03
{% endraw %} {% raw %}
lr = 1e-3
lrs = slice(lr)
epoch = 3
pct_start = 0.3
wd = 1e-3
save_fname = model_name
{% endraw %} {% raw %}
callbacks = [ShowGraph(learn), SaveModelCallback(learn, name=save_fname)]
{% endraw %} {% raw %}
learn.fit_one_cycle(epoch, lrs, pct_start=pct_start, wd=wd, callbacks=callbacks)
epoch train_loss valid_loss m_psnr m_ssim time
0 0.133611 0.082961 29.108580 0.399737 01:01
1 0.115990 0.090927 28.044077 0.434667 00:42
2 0.106556 0.070284 33.024979 0.475144 00:43
Better model found at epoch 0 with valid_loss value: 0.08296076953411102.
Better model found at epoch 2 with valid_loss value: 0.07028444111347198.
{% endraw %} {% raw %}
learn.show_results(cmap='gray')
{% endraw %}

Test

{% raw %}
test_hr = set14_hr
{% endraw %} {% raw %}
il_test_x = ImageImageList.from_folder(test_hr, convert_mode='YCbCr', after_open=partial(after_open_image, size=in_size, scale=4, luminance=True))
il_test_y = ImageImageList.from_folder(test_hr, convert_mode='YCbCr', after_open=partial(after_open_image, size=out_size, luminance=True))
il_test_x_up = ImageImageList.from_folder(test_hr, convert_mode='YCbCr', after_open=partial(after_open_image, size=out_size, scale=4, sizeup=True, luminance=True))
{% endraw %} {% raw %}
sr_test_upscale(learn, il_test_x, il_test_y, il_test_x_up, model_name, cmap='gray')
# Official: bicubic PSNR:25.99, SSIM:0.7486
bicubic: PSNR:24.48,SSIM:0.7819
EPSCN:	 PSNR:23.88,SSIM:0.7717
{% endraw %}

Report

{% raw %}
model
EPSCN(
  (conv1): Conv2d(1, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
  (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (conv3): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (conv4): Conv2d(32, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (pixel_shuffle): PixelShuffle(upscale_factor=4)
)
{% endraw %} {% raw %}
learn.summary()
EPSCN
======================================================================
Layer (type)         Output Shape         Param #    Trainable 
======================================================================
Conv2d               [64, 64, 64]         1,664      True      
______________________________________________________________________
Conv2d               [64, 64, 64]         36,928     True      
______________________________________________________________________
Conv2d               [32, 64, 64]         18,464     True      
______________________________________________________________________
Conv2d               [16, 64, 64]         4,624      True      
______________________________________________________________________
PixelShuffle         [1, 256, 256]        0          False     
______________________________________________________________________

Total params: 61,680
Total trainable params: 61,680
Total non-trainable params: 0
Optimized with 'torch.optim.adam.Adam', betas=(0.9, 0.99)
Using true weight decay as discussed in https://www.fast.ai/2018/07/02/adam-weight-decay/ 
Loss function : FlattenedLoss
======================================================================
Callbacks functions applied 
{% endraw %}